<template>
  <div class="mycard">
    <el-card class="card">
      <div class="header">
        <h2>员工导入</h2>
      </div>
      <div class="attention">
        <span>
          <i class="el-icon-warning"></i>
          <span
            >每次导入仅可添加1000名员工，姓名、手机、入职时间、聘用形式为必填项</span
          >
        </span>
      </div>
      <uploadexcel
        :beforeUpload="beforeUpload"
        :onSuccess="onSuccess"
      ></uploadexcel>
    </el-card>
  </div>
</template>

<script>
import { uploadExcelApi } from '../../../api/employees'
export default {
  methods: {
    beforeUpload (file) {
      const less2M = file.size / 1024 / 1024 < 2
      if (less2M) {
        return less2M
      }
      return !less2M
    },
    dataformat (day) {
      const time = new Date((day - 1) * 24 * 60 * 60 * 1000 + 1)
      time.setYear(time.getFullYear() - 70)
      return time.toString()
    },
    async onSuccess ({ results }) {
      const obj = {
        入职日期: 'timeOfEntry',
        姓名: 'username',
        工号: 'workNumber',
        手机号: 'mobile',
        转正日期: 'correctionTime'
      }

      const newList = results.map(item => {
        var newList = {}
        Object.keys(obj).forEach(subitem => {
          if (
            obj[subitem] === 'timeOfEntry' ||
            obj[subitem] === 'correctionTime'
          ) {
            newList[obj[subitem]] = this.dataformat(item[subitem])
          } else {
            newList[obj[subitem]] = item[subitem]
          }
        })
        return newList
      })
      await uploadExcelApi(newList)
      this.$message.success('新增成功')
      this.$router.push('/employees')
    }
  }
}
</script>

<style lang="scss" scoped>
.mycard {
  padding: 20px 20px;
  .card {
    padding-bottom: 80px;
  }
  .header {
    display: flex;
    justify-content: center;
  }
  .attention {
    border-radius: 3px;
    padding: 10px 9px;
    background-color: #fdf6ec;
    color: #e6a23c;
  }
}
</style>
